#include "HandleCmdCodec.h"

HandleCmdCodec::HandleCmdCodec()
{}

HandleCmdCodec::~HandleCmdCodec()
{}

HandleCmd HandleCmdCodec::getCmd()
{
  //cmd.reset();
  HandleCmd cmd;
  //receive
  size_t n = Serial.readBytes(buffer, 8);
  if(n != 8) {
    return cmd;
  }
  delay(kReceiveDelay);
  //decode and verify
  cmd.decode(buffer);
  if(!cmd) {
    //debug print
    #ifdef DEBUG_MODE
    Serial.print("invalid cmd: ");
    Serial.println(cmd.cmdId);
    printCmd();
    #endif
  }
  return cmd;
}

void HandleCmdCodec::printCmd()
{
  for(int i=0; i<8; ++i) {
    Serial.print(buffer[i], HEX);
    Serial.print(" ");
  }
  Serial.println("");
}
